package convert

import (
	"errors"
	"math"
	"strconv"
	"unsafe"
)

// Int64ToInt 将int64类型转换为int类型，先检查是否在int范围内，然后进行转换
func Int64ToInt(value int64) (int, error) {
	if value < math.MinInt || value > math.MaxInt {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int(value), nil
}

// Int64ToInt8 将int64类型转换为int8类型，先检查是否在int8范围内，然后进行转换
func Int64ToInt8(value int64) (int8, error) {
	if value < math.MinInt8 || value > math.MaxInt8 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int8(value), nil
}

// Int64ToInt16 将int64类型转换为int16类型，先检查是否在int16范围内，然后进行转换
func Int64ToInt16(value int64) (int16, error) {
	if value < math.MinInt16 || value > math.MaxInt16 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int16(value), nil
}

// Int64ToInt32 将int64类型转换为int32类型，先检查是否在int32范围内，然后进行转换
func Int64ToInt32(value int64) (int32, error) {
	if value < math.MinInt32 || value > math.MaxInt32 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int32(value), nil
}

// Int64ToUint 将int64类型转换为uint类型，先检查是否为负数，然后检查是否在uint范围内，然后进行转换
func Int64ToUint(value int64) (uint, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint")
	}
	if unsafe.Sizeof(uint(0)) == 4 && value > math.MaxUint32 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint(value), nil
}

// Int64ToUint8 将int64类型转换为uint8类型，先检查是否为负数，然后检查是否在uint8范围内，然后进行转换
func Int64ToUint8(value int64) (uint8, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint8")
	}
	if value > math.MaxUint8 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint8(value), nil
}

// Int64ToUint16 将int64类型转换为uint16类型，先检查是否为负数，然后检查是否在uint16范围内，然后进行转换
func Int64ToUint16(value int64) (uint16, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint16")
	}
	if value > math.MaxUint16 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint16(value), nil
}

// Int64ToUint32 将int64类型转换为uint32类型，先检查是否为负数，然后检查是否在uint32范围内，然后进行转换
func Int64ToUint32(value int64) (uint32, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint32")
	}
	if value > math.MaxUint32 {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint32(value), nil
}

// Int64ToUint64 将int64类型转换为uint64类型，先检查是否为负数，然后直接转换
func Int64ToUint64(value int64) (uint64, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint64")
	}
	return uint64(value), nil
}

// Int64ToFloat32 将int64类型转换为float32类型，直接转换
func Int64ToFloat32(value int64) (float32, error) {
	return float32(value), nil
}

// Int64ToFloat64 将int64类型转换为float64类型，直接转换
func Int64ToFloat64(value int64) (float64, error) {
	return float64(value), nil
}

// Int64ToString 将int64类型转换为string类型，使用strconv.FormatInt函数进行转换
func Int64ToString(value int64) string {
	return strconv.FormatInt(value, 10)
}

// Int64ToBool 将int64类型转换为bool类型，非零值转换为true，零值转换为false
func Int64ToBool(value int64) bool {
	return value != 0
}
